<?php
namespace Home\Controller;

use data\Service\goods\Product as GoodsService;
use data\Service\cart\ShopCart as CartService;
use data\Service\BaseService as BaseService;
use data\Service\store\StoreService as StoreService;
use data\Service\goods\ShopArea as SpAreaService;
use data\Service\pay\PayMent as PayMentService;
use Think\Controller;
use Home\Controller\CommonController;
 
class SpCartController extends CommonController {
	protected $tb_store = 'store';//店铺表	
	protected $tb_goods = 'product';//产品数据表
	protected $tb_cart = 'shop_cart';//购物车表	
	private $goods = null;
	private $spcart = null;
	private $Base = null;
	private $sparea = null;
	private $payment = null;
	private $store = null;	
	
	/**
	*会员购物车
	*/
	private function user_shop_cart($g_list, $goods_stock, $specArr) {
		
		$this->spcart = new CartService();
		$this->spcart->historyCartAdd($goods_stock);		
		$this->spcart->getAddCar($g_list, $specArr);	
		
	}

	/**
	*加入购物车
	*/
	public function add_cart() {

		if (I('post.goods_id') && I('post.quantity') && I('post.type')=='cart_add') {
		
			if(!I('post.colorid') && !I('post.specid')){
				if(!I('post.color_n') && !I('post.spec_n')){
					$this->ajaxReturn(array('msg'=>'请选择规格'));
				}
			}
			
			//查找产品			
			$goods_num = I('post.quantity') ;			
			$this->goods = new GoodsService();

			$key = I('post.colorid').'|'.I('post.specid');
			
			$g_list = $this->goods->getGoodsAmount($goods_num, I('post.goods_id'), $key);
			
			if (false===$g_list) {
				$this->ajaxReturn(array('msg'=>'产品已下架'));
			} else if (1===$g_list) {
				$this->ajaxReturn(array('msg'=>'库存不足'));
			}
			
			$specArr = 0 ;
			
			if ($g_list['goods_spec_arr'][$key]['price']) {
				$specArr = $g_list['goods_spec_arr'][$key] ;
			}		
			
			//店铺名称			
			$this->store = new StoreService();
			unset($str);
			unset($arr);
			unset($field);
			$str = 'store_id=%d' ;
			$arr = array($g_list['st_id']) ;
			$field = "store_id,store_name" ;
			$storeInfo = $this->store->getStoreInfo($str, $arr, $field);
			$g_list['store_name'] = $storeInfo ['store_name'];
			
			//产品库存量
			if ($key) {
				$goods_stock = floatval($g_list['goods_spec_arr'][$key]['stock']) ;
			} else {
				$goods_stock = floatval($g_list['goods_storage']) ;
			}
			
			//判断分区
			$g_list['shop_area_ids'] = unserialize($g_list['shop_area_ids']);
			$this->sparea = new SpAreaService();
			unset($str);
			unset($arr);
			unset($field);
			$str = 'shop_area_id=%d and shop_area_state=%d' ;
			$arr = array($g_list['shop_area_ids'][0],1) ;
			$field = "pay_id" ;
			$payid = $this->sparea->getSpAreaInfo($str, $arr, $field);
			if (!$payid) {
				$this->ajaxReturn(array('msg'=>'该产品所在分区已关闭'));
			}
			
			//支付方式
			$this->payment = new PayMentService();
			unset($str);
			unset($arr);
			unset($field);
			$str = 'payment_id=%d and payment_state=%d' ;
			$arr = array($payid['pay_id'],1) ;
			$field = "*" ;
			
			$paycode = $this->payment->getPayInfo($str, $arr, $field);
			if (!$paycode) {
				$this->ajaxReturn(array('msg'=>'暂不支持'.$paycode['payment_name'].',请选择其他商品'));
			}
		
			//支付字段
			$g_list = $this->payment->getPayCodeName($paycode['payment_code'], $g_list, $specArr);
			
			//不能购买自己的产品
			if($this->store->storeBuyChack($g_list['st_id'])){			
				$this->ajaxReturn(array('msg'=>'不能购买自己店铺的产品'));	
			}
			
			$this->Base = new BaseService();
			
			//加入购物车				
			if ($_SESSION['user_id']) {
			
				//会员登录状态
				if (I('post.change')) {
					$g_list ['change'] = I('post.change') ;
				}
				//购买数量
				$g_list ['goods_num'] = $goods_num ;
				//加入购物车数据表
				
				$this->user_shop_cart($g_list, $goods_stock, I('post.'));			
				
				$history = $this->Base->strAnalysis(session('_cart')) ;
			} else {
				//浏览器
				$history =  $this->Base->strAnalysis(cookie('_cart')) ;
			}
			
			//将数据保存到cookie或者session
			$all_goods = array() ;
			if ($history[0]['goods_id']) {
				$all_goods = $history ;
			}
			//$this->ajaxReturn($all_goods);
			$this->spcart = new CartService();
		    $result = $this->spcart->shopCartDtata($all_goods, $g_list, $goods_num, I('post.')) ;

			$this->ajaxReturn($result);
		}
		
	}
	

	/**
	*加入购物车2
	*/
	public function add_cart_2() {

		if (I('post.cart_id') && I('post.quantity') && I('post.type')=='cart_add' && $_SESSION['user_id']) {
			
			$this->spcart = new CartService();
			$this->goods = new GoodsService();
			//查找购物车			
			$goods_num = I('post.quantity') ;			
			$str = 'cart_id=%d' ;
			$arr = array(I('post.cart_id')) ;
			$field = "*" ;
					
			$g_list = $this->spcart->getCartInfo($str, $arr, $field);
			
			$goods_num = $goods_num - $g_list['goods_num'] ;	
			//查找产品					
			//产品库存量
			$key = I('post.colorid').'|'.I('post.specid');
			
			$goodsInfo = $this->goods->getGoodsAmount($goods_num, $g_list['goods_id'], $key);
			
			if (false===$goodsInfo) {
				$this->ajaxReturn(array('msg'=>'产品已下架'));
			} else if (1===$goodsInfo) {
				$this->ajaxReturn(array('msg'=>'库存不足'));
			}
			
/* 			$goodsInfo['price'] = $g_list['goods_price'];
			$goodsInfo['point_num'] = $g_list['point_num'];
			$goodsInfo['coin_num'] = $g_list['coin_num'];	 */
			
			$this->Base = new BaseService();

			//加入购物车数据表
			$edit = $this->spcart->getEditCar($g_list, $goods_num, 0, 0);					
			//$this->ajaxReturn($goods_num);
			$newNum = $goods_num + $g_list['goods_num'] ;

			$this->ajaxReturn(array('num'=>$newNum, 'state'=>1, 'msg'=>'成功'));
		}
		
	}
	
	/**
	*购物车内容ajax
	*/
	public function ajax_cart() {
		
		if (I('get.act')=='cart' && I('get.op')=='ajax_load' && I('get.type')) {
			
			unset($cart);
			//购物车列表
			$this->spcart = new CartService();
			$cart = $this->spcart->ajaxCart();	
			
			if ($_SESSION['user_id'] && !$cart['cart_list'][0]) {
				//更新购物车
				$this->user_shop_cart(0);
			    $cart = $this->spcart->updateCarData();	
			}
						
			$this->assign('cart_type',I('get.type'));			
			$this->assign('cart_list',$cart);
			$this->display();
	    }
	}	


	/**
	*删除购物车信息
	*/
	public function del_cart() {
		
		if (I('get.act')=='cart' && I('get.op')=='del' && I('get.goods_id')) {

			$this->Base = new BaseService();
			
			if ($_SESSION['user_id']) {
 				//会员登录状态 删除购物车
				
				$this->Base->delCart(I('post.goods_id'));
				
				$this->spcart = new CartService();
				$str = "cart_id=%d";
				$arr= array(I('post.cart_id'));
				$del = $this->spcart->getCartDel($str, $arr);
				
			} else {
				//浏览器保存购物车信息
				$this->Base->delCookieCart(I('post.goods_id'));
			}
			
			$result = array('state'=>1, 'quantity'=>$_COOKIE['_cart_goods_num'], 'amount'=>$_COOKIE['_cart_goods_amount'], 'amount_points'=>$_COOKIE['_cart_goods_amount_points'],  'amount_coin'=>$_COOKIE['_cart_goods_amount_coins'], 'msg'=>'删除失败');	
			$this->ajaxReturn($result);
		}	
			
	}
	
	/**
	*购物车列表
	*/
	public function cartlist() {
	
		if (I('get.type')=='shop_cart') {
			
			if (!$_SESSION['user_id']) {
				 $this->redirect('/login','请先登录！');
			}		
			$this->spcart = new CartService();
			$result = $this->spcart->getUserCartList();
				
		    //dump($result);die;
			
			$this->assign('amount_points',$result['amount_points']);
			$this->assign('total',$result['total']);
			$this->assign('cart_list',$result['cart_list']);
			$this->display();			
		} else {
			 $this->redirect('/login','请先登录！');
		}
	}
	
}